package com.example.communist_app_api.controller.web;

import com.example.communist_app_api.config.mq.NoticeTask;
import com.example.communist_app_api.db.dto.MessageAuditDTO;
import com.example.communist_app_api.db.pojo.User;
import com.example.communist_app_api.exception.GlobalException;
import com.example.communist_app_api.service.MessageService;
import com.example.communist_app_api.service.UserService;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.web.req.*;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/3/23 23:20
 */
@RestController
@RequestMapping("/messageAudit")
@Api("建言献策审核")
public class MessageAuditController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;

    @Autowired
    private NoticeTask noticeTask;


    @PostMapping("/showAllAuditorMessage")
    @ApiOperation("显示所有待审核的留言")
    public R showAllAuditorMessage(HttpServletRequest httpServletRequest ,@Valid @RequestBody MessageAuditShowReq req){
        Integer userId = getUserIdByTokenAndWeb(httpServletRequest);
        PageInfo<MessageAuditDTO> pageInfo = messageService.selectAllMessageByAudit(req,userId);
        return R.ok().put("pageInfo", pageInfo);
    }

    //审批
    @ApiOperation("建言献策审批成功")
    @PostMapping("/AuditMessage/success")
    @Transactional
    public R AuditPersonalMessageAuditSuccess(HttpServletRequest httpServletRequest,@Valid @RequestBody AuditMessageSuccessReq req) {
        Integer messageId = req.getMessageId();
        //web端登录的人
        Integer auditorId = getUserIdByTokenAndWeb(httpServletRequest);
        Integer status = messageService.updateAuditMessage(messageId, auditorId);
        if (status == 0) {
            throw new GlobalException("更改信息失败");
        }
        String message = "";
        if (req.getMessage() == null) {
            message = "您提交的建言献策已审核成功，已上传到党建小程序建言献策区域!";
        } else {
            message = req.getMessage();
        }
        noticeTask.sendSystemNotice(String.valueOf(req.getUserId()), message);
        return R.ok("建言献策审批成功");
    }

    @ApiOperation("建言献策审批不合格")
    @PostMapping("/AuditMessage/fail")
    @Transactional
    public R AuditPersonalMessageAuditFail(@Valid @RequestBody AuditMessageFailReq req) {
        Integer messageId = req.getMessageId();
        //建言献策图片有问题
        List<String> photoUrlList = new ArrayList<>();
        String message = "";
        if(req.getPhotoUrlList().size()!=0&&req.getPhotoUrlList()!=null){
             photoUrlList = req.getPhotoUrlList();
            String photoUrl = String.join(",", photoUrlList);

            message = Optional.ofNullable(req.getMessage()+" 您提交的素材已审核失败，具体照片是：" + photoUrl + "请点击链接查看照片")
                    .orElse("您提交的素材已审核失败，具体照片是：" + photoUrl + "请点击链接查看照片");
        }else{
            //建言献策文字有问题
            message = "您发表的文字不适！ "+req.getMessage();
        }

        //流程是：删除该条建言献策信息，并把审核不成功的信息返回给用户请求用户重新提交
        messageService.deleteMessageByMessageId(messageId);
        noticeTask.sendSystemNotice(String.valueOf(req.getUserId()), message);
        return R.ok("建言献策不通过");
    }

    private  Integer getUserIdByTokenAndWeb(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String username = claims.getSubject();
        User userInfo = userService.getUserInfo(username);
        return userInfo.getId();
    }
}
